package com.guigu.abo.config;

import com.guigu.abo.controllers.admin.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.*;

import java.util.List;

@Configuration
@ComponentScan("com.guigu.abo.controllers")
@EnableWebMvc//替代注解驱动
public class SpringMvcConfig implements WebMvcConfigurer {
    /*视图解析器*/

    public void configureViewResolvers(ViewResolverRegistry registry){
        //默认配了jsp        registry.jsp();
        registry.jsp("WEB-INF/",".jsp");
    }

    /*静态资源处理器*/
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");/*放行特定文件夹下的*/
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");/*放行特定文件夹下的*/
        registry.addResourceHandler("/images/**").addResourceLocations("/images/");/*放行特定文件夹下的*/
        registry.addResourceHandler("/fonts/**").addResourceLocations("/fonts/");/*放行特定文件夹下的*/
        registry.addResourceHandler("/files/**").addResourceLocations("/files/");/*放行特定文件夹下的*/
    }

    /*异常处理器*/
    public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers){
//        /*如果需要使用 直接new 处理器类对象*/
//        resolvers.add()
    }

    /*文件上传器*/
    @Bean(name ="multipartResolver" )
    public CommonsMultipartResolver commonsMultipartResolver(){
        CommonsMultipartResolver multipartResolver =
                new CommonsMultipartResolver();
        multipartResolver.setDefaultEncoding("UTF-8");
        multipartResolver.setMaxUploadSize(500000);//上传文件大小限额
        multipartResolver.setMaxInMemorySize(3145728);//单文件或多文件限额
        return multipartResolver;
    }

    /*拦截器*/
    public void addInterceptors(InterceptorRegistry registry){
        /*registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**").excludePathPatterns("/userLogin");*/

/*        registry.addInterceptor(new 自己写的拦截器类对象名称)
                .addPathPatterns("/**").excludePathPatterns("/userLogin");
        .excludePathPatterns("/userLogin") 剔除不需要拦截的请求 比如登录
*/
    }


}
